package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StreamUtils;
import com.joker.common.utils.StringUtils;
import com.joker.orange.domain.NovelChapter;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.CaricatureChapterBo;
import com.joker.orange.domain.vo.CaricatureChapterVo;
import com.joker.orange.domain.CaricatureChapter;
import com.joker.orange.mapper.CaricatureChapterMapper;
import com.joker.orange.service.ICaricatureChapterService;

import java.math.BigDecimal;
import java.util.*;

/**
 * 漫画章节Service业务层处理
 *
 * @author Joker
 * @date 2024-06-09
 */
@RequiredArgsConstructor
@Service
public class CaricatureChapterServiceImpl implements ICaricatureChapterService {

    private final CaricatureChapterMapper baseMapper;

    /**
     * 查询上一章节
     */
    @Override
    public CaricatureChapterVo selectLastChapterById(CaricatureChapterBo bo) {
        return baseMapper.selectLastChapterById(bo);
    }

    /**
     * 查询下一章节
     */
    @Override
    public CaricatureChapterVo selectNextChapterById(CaricatureChapterBo bo) {
        return baseMapper.selectNextChapterById(bo);
    }

    /**
     * 查询漫画章节
     */
    @Override
    public CaricatureChapterVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询漫画章节列表
     */
    @Override
    public TableDataInfo<CaricatureChapterVo> queryPageList(CaricatureChapterBo bo, PageQuery pageQuery) {
        QueryWrapper<CaricatureChapter> wrapper = Wrappers.query();
        wrapper.eq(ObjectUtil.isNotNull(bo.getCaricatureId()), "c.caricature_id", bo.getCaricatureId())
                .like(StringUtils.isNotBlank(bo.getChapterTitle()), "c.chapter_title", bo.getChapterTitle())
                .eq(StringUtils.isNotBlank(bo.getChargeType()), "c.charge_type", bo.getChargeType());
        Page<CaricatureChapterVo> result = baseMapper.queryPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(result);
    }

    /**
     * 查询漫画章节列表
     */
    @Override
    public List<CaricatureChapterVo> queryList(CaricatureChapterBo bo) {
        LambdaQueryWrapper<CaricatureChapter> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CaricatureChapter> buildQueryWrapper(CaricatureChapterBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CaricatureChapter> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCaricatureId() != null, CaricatureChapter::getCaricatureId, bo.getCaricatureId());
        lqw.eq(StringUtils.isNotBlank(bo.getChapterUrl()), CaricatureChapter::getChapterUrl, bo.getChapterUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getChapterTitle()), CaricatureChapter::getChapterTitle, bo.getChapterTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getChargeType()), CaricatureChapter::getChargeType, bo.getChargeType());
        lqw.eq(bo.getConsumeDiamond() != null, CaricatureChapter::getConsumeDiamond, bo.getConsumeDiamond());
        return lqw;
    }

    /**
     * 新增漫画章节
     */
    @Override
    public Boolean insertByBo(CaricatureChapterBo bo) {
        CaricatureChapter add = BeanUtil.toBean(bo, CaricatureChapter.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean insertBatch(List<CaricatureChapter> caricatureChapterList) {
        return baseMapper.insertBatch(caricatureChapterList);
    }

    /**
     * 修改漫画章节
     */
    @Override
    public Boolean updateByBo(CaricatureChapterBo bo) {
        CaricatureChapter update = BeanUtil.toBean(bo, CaricatureChapter.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CaricatureChapter entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除漫画章节
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<CaricatureChapterVo> selectCaricatureChapterByCaricatureId(Long caricatureId) {
        QueryWrapper<CaricatureChapter> wrapper = Wrappers.query();
        wrapper.eq(ObjectUtil.isNotNull(caricatureId), "c.caricature_id", caricatureId);
        return baseMapper.queryPageList(wrapper);
    }

    @Override
    public void updateBatchChapter(String chargeType, String consumeDiamond, Long[] chapterIds) {
        if (ArrayUtil.isNotEmpty(chapterIds)) {
            // 批量修改
            List<CaricatureChapter> list = StreamUtils.toList(Arrays.asList(chapterIds), chapterId -> {
                CaricatureChapter caricatureChapter = new CaricatureChapter();
                caricatureChapter.setId(chapterId);
                caricatureChapter.setChargeType(chargeType);
                if ("2".equals(chargeType)) {
                    caricatureChapter.setConsumeDiamond(new BigDecimal(consumeDiamond));
                }
                return caricatureChapter;
            });
            baseMapper.updateBatchById(list);
        }
    }
}
